<template>
  <el-dialog
    :title="!form.id ? '新增' : '修改'"
    :close-on-click-modal="false"
    :visible.sync="visible"
    width="500px"
  >
    <el-form
      :model="form"
      :rules="rules"
      size="mini"
      ref="form"
      @keyup.enter.native="submitForm()"
      label-width="80px"
    >
      <el-form-item label="采购商品" prop="skuId">
        <sku-select v-model="form.skuId"></sku-select>
      </el-form-item>
      <el-form-item label="采购数量" prop="skuNum">
        <el-input v-model="form.skuNum" type="number" placeholder="采购数量"></el-input>
      </el-form-item>
      <el-row>
        <el-col :span="12">
          <el-form-item label="目标仓库" prop="wareId">
            <el-select style="width: 100%" v-model="form.wareId" placeholder="请选择仓库" clearable>
              <el-option :label="item.name" :value="item.id" v-for="item in wareList" :key="item.id"></el-option>
            </el-select>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="采购状态" prop="status">
            <el-select v-model="form.status" placeholder="请选择状态">
              <el-option
                v-for="dict in dict.type.purchase_detail_status"
                :key="dict.value"
                :label="dict.label"
                :value="parseInt(dict.value)"
              ></el-option>
            </el-select>
          </el-form-item>
        </el-col>
      </el-row>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button size="mini" icon="el-icon-circle-check" type="primary" @click="submitForm()">确定</el-button>
      <el-button size="mini" icon="el-icon-circle-close" @click="visible = false">取消</el-button>
    </span>
  </el-dialog>
</template>

<script>
import {getPurchaseDetail, savePurchaseDetail, updatePurchaseDetail} from '@/api/ware/purchaseDetail'
import {wareList} from "@/api/ware/ware";
import SkuSelect from "@/views/common/skuSelect.vue";
import {skuList} from "@/api/product/sku";

export default {
  components: {SkuSelect},
  dicts: ['purchase_detail_status'],
  data() {
    return {
      visible: false,
      wareList: [],
      skuList: [],
      form: {},
      rules: {
        skuId: [
          {required: true, message: "采购商品id不能为空", trigger: "blur"}
        ],
        skuNum: [
          {required: true, message: "采购数量不能为空", trigger: "blur"}
        ],
        wareId: [{required: true, message: "请选择仓库", trigger: "blur"}]
      }
    };
  },
  created() {
    this.getWares()
  },
  methods: {
    getWares() {
      let params = {
        pageNo: 1,
        pageSize: 500
      }
      wareList(params).then(res => {
        if (res.success) {
          this.wareList = res.data.records;
        }
      });
    },
    edit(id) {
      this.visible = true;
      this.$nextTick(() => {
        this.$refs["form"].resetFields();
        getPurchaseDetail(id).then(res => {
          if (res.success) {
            this.form = res.data
          }
        });
      });
    },
    add(purchaseId) {
      this.skuId = ''
      this.visible = true;
      this.form.status = 0
      this.form.skuId = ''
      this.form.purchaseId = purchaseId
    },
    // 表单提交
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (!this.form.id) {
            savePurchaseDetail(this.form).then(res => {
              if (res.success) {
                this.$message.success('新增成功')
                this.visible = false
                this.$emit("ok");
              }
            })
          } else {
            updatePurchaseDetail(this.form).then(res => {
              if (res.success) {
                this.$message.success('修改成功')
                this.visible = false
                this.$emit("ok");
              }
            })
          }
        }
      });
    }
  }
};
</script>
